package com.example.a22310.myapplication.Model;

import com.example.a22310.myapplication.Beans.Post;
import com.example.a22310.myapplication.Beans.PostComment;
import com.example.a22310.myapplication.Beans.PostDetail;
import com.example.a22310.myapplication.Listener.PostListener;
import com.example.a22310.myapplication.Service.service;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.provider.ContactsContract.CommonDataKinds.Website.URL;

public class PostModel {
    private Retrofit retrofit;
    public PostModel()
    {
        retrofit= new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void postList(final PostListener<Post> listener) {  //帖子列表
        service userService = retrofit.create(service.class);
        Call<Post> call = userService.postlist();
        Callback<Post> callback = new Callback<Post>() {
            @Override
            public void onResponse(Call<Post> call, Response<Post> response) {
                Post post = response.body();
                listener.onSuccess(post);
            }
            @Override
            public void onFailure(Call<Post> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    public void postDetail(final PostListener<PostDetail> listener, int postid) {  //帖子详情
        service userService = retrofit.create(service.class);
        Call<PostDetail> call = userService.postDetail(postid);
        Callback<PostDetail> callback = new Callback<PostDetail>() {
            @Override
            public void onResponse(Call<PostDetail> call, Response<PostDetail> response) {
                PostDetail post = response.body();
                listener.onSuccess(post);
            }
            @Override
            public void onFailure(Call<PostDetail> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    public void postComm(final PostListener<PostComment> listener, int userid, int postid, int flag) {  //帖子评论
        service userService = retrofit.create(service.class);
        Call<PostComment> call = userService.thiscommentList(userid,postid,flag);
        Callback<PostComment> callback = new Callback<PostComment>() {
            @Override
            public void onResponse(Call<PostComment> call, Response<PostComment> response) {
                PostComment post = response.body();
                listener.onSuccess(post);
            }
            @Override
            public void onFailure(Call<PostComment> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

}
